package com.hl.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 解析指令
 * @author Administrator
 *
 */
public class AnalysisStr {
	//查询数据库
	private DBHelper dbHelper = new DBHelper();
	private Connection connection = null;
	
	public CodeAttribute analy(String info,CodeAttribute code) throws Exception{
		int length = info.length(); //获取指令长度
		if(length <= 0) {return null;}
		System.out.println("收到客户端发来的消息长度为：[" + length + "] 指令为:  " + info +" 开始解析指令...");

		String str [] = info.split(":");  //拆分字符串

		System.err.println("拆分成功-->拆分后的长度为::" + str.length );
		//判断发来的指令是否有前缀后缀 没有视为无效数据
		if(!("[".equalsIgnoreCase(str[0]) && "]".equalsIgnoreCase(str[str.length-1]) && length >= 9)) {return null;}
		if(code == null) { //wifi模块和安卓编号实体类
			code = new CodeAttribute();
		}
		int res = clientOnlyIdIsExistence(str[2]);  //查询数据库是否存在
		if(res <= 0) {System.out.println("设备id不存在");return null;}
		code.setId(res);  //保存id
		
		code.setInstructionCode(info); //指令码
		code.setClientTypeNumber(str[1]); //指令编码
		code.setInstructionLength(str.length); //设置指令长度
		//开始解析客户端编号 
		code.setClientTypeNumber(str[1]);  //指令编号
		code.setClientOnlyId(str[2]); //客户端唯一id
		updateStatu(code.getClientOnlyId(),1); //更新在线状态


		if(str.length <= 4) { save(code); return code;} //如果长度大于等于4表示后面还有数据 继续解析
		code.setSendOnlyId(str[3]); //发给客户端唯一编号 a-1 w-1
		code.setSendData(str[4]); //发送的数据

		save(code);  //保存数据	
		return code;
	}

	/**
	 * 查询客户端是否存在
	 * @param clientOnlyId
	 * @return
	 */
	public Integer clientOnlyIdIsExistence(String clientOnlyId) {
		String result = null;
		try {
			String sql = "SELECT id FROM `wifi` WHERE `clientOnlyId` = ?";
			connection = dbHelper.getConnection();
			BaseDao dao = new BaseDao(connection);
			result =  dao.executeObject(sql, clientOnlyId).toString();
		} catch (Exception e) {
			System.out.println("clientOnlyIdIsExistence 客户端id查询失败..." + e);
			return 0;
		}finally {
			try {
				connection.close();
			} catch (SQLException e) {
				System.out.println("connection  关闭失败...");
			}
		}
		return Integer.parseInt(result);
	}
	/**
	 * 保存数据
	 * @param code
	 * @return
	 */
	public Integer save(CodeAttribute code) {
		int res = -1;
		try {
			DBHelper dbHelper = new DBHelper();
			String sql = "INSERT INTO `instructionRecording`(`wifi_id`,`instructionCode`,`sendOnlyId`,sendData) VALUES(?,?,?,?)";
			connection = dbHelper.getConnection();
			BaseDao dao = new BaseDao(connection);
			res =  dao.executeUpdate(sql, code.getId(),code.getInstructionCode(),code.getSendOnlyId(),code.getSendData());
		} catch (Exception e) {
			System.out.println("save 插入出错" + e);
		}finally {
			try {
				connection.close();
			} catch (SQLException e) {
				System.out.println("connection  关闭失败...");
			}
		}
		return res;
	}
	
	/**
	 * 更新在线状态
	 * @param code
	 */
	public void updateStatu(String clientOnlyId,int statu) {
		int res = -1;
		try {
			DBHelper dbHelper = new DBHelper();
			String sql = "UPDATE `wifi` SET `statu` = ?,updateTime = ? WHERE `clientOnlyId` = ?";
			connection = dbHelper.getConnection();
			BaseDao dao = new BaseDao(connection);
			res = dao.executeUpdate(sql,statu,new Date(),clientOnlyId);
			System.out.println("更新客户端" + (res > 0 ? "成功":"失败"));
		} catch (Exception e) {
			System.out.println("更新失败 客户端唯一id为: " + clientOnlyId);
		}
	}
}

